package com.tbit.uqbike.service.base.impl;

import com.tbit.uqbike.service.base.RedisService1;
import com.tbit.uqbike.webmanager.util.redis.RedisUtils;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service("redisService1")
public class RedisServiceImpl1 implements RedisService1 {
    @Autowired
    private RedisUtils redisUtil;

    @Override
    public void add(String key, String value) {
        redisUtil.set(key, value);
    }

    @Override
    public String get(String key) {
        return redisUtil.get(key);
    }

    @Override
    public void del(String key) {
        redisUtil.delete(key);
    }

    @Override
    public String get(String hashId, String key) {
        return (String) redisUtil.hGet(hashId, key);

    }

    @Override
    public Map<Object, Object> getAll(String hashId) {
        return redisUtil.hGetAll(hashId);
    }

    @Override
    public void incr(String hashId) {
        if (!hasKey(DateTimeUtil.getNowTimeYMD() + "." + hashId)) {
            redisUtil.set(DateTimeUtil.getNowTimeYMD() + "." + hashId, "1");
            return;
        } else {
            int count = Integer.parseInt(redisUtil.get(DateTimeUtil.getNowTimeYMD() + "." + hashId)) + 1;
            redisUtil.set(DateTimeUtil.getNowTimeYMD() + "." + hashId, "" + count);
        }
    }

    @Override
    public Boolean hasKey(String hashId) {
        return redisUtil.hasKey(hashId);
    }

    @Override
    public Long rPush(String key, String value) {
        return redisUtil.lRightPush(key, value);
    }

    @Override
    public void add(String key, String value, Integer seconds) {
        redisUtil.setEx(key, value, seconds, TimeUnit.SECONDS);

    }
}  